from diff import *
import numpy as np

point = []
for pi in range(len(diff1)):
# 假设stock_point是你要查找的股票斜率在diff1中的位置
    stock_point = pi  # 这里只是一个示例值，你需要设置为实际值
    n = 4 # 你希望查找的前后位置数量

# 获取stock_point前后n个位置的ETF斜率
    etf_slopes = diffETF[max(0, stock_point - n): stock_point + n + 1]

# 初始化最小距离和对应的位置
    min_distance = float('inf')
    min_index = None

# 对于每一个ETF斜率，计算与股票斜率的距离
    for i, etf_slope in enumerate(etf_slopes):
        current_distance = abs(etf_slopes[i]-diff1[stock_point])
        if current_distance < min_distance:
            min_distance = current_distance
            min_index = i + stock_point - n if stock_point - n >= 0 else i  # 记录最小距离对应的位置
    point.append((pi,min_index))
# 输出最相似点的位置
#print(f"最相似的点在diffETF中的位置为: {min_index}")
print(point)


zhihoujieshu = []  # 用于存储结果的列表

# 遍历每个元组，计算两个数的差值，并将结果添加到results列表中
for tup in point:
    num1 = tup[0]
    num2 = tup[1]
    if num2 is not None:  # 检查num2是否为None，跳过为None的元组
        difference = num2 - num1  # 计算差值
        zhihoujieshu.append(difference)  # 将差值添加到results列表中
zhihoujieshu = [abs(x) for x in zhihoujieshu]
# 打印结果
print(zhihoujieshu)

from collections import Counter


# 使用Counter来计算每个数字出现的次数
counts = Counter(zhihoujieshu)

# 找出出现次数最多的数字，即众数
mode = counts.most_common(1)[0][0]

# 计算众数的占比
a = counts[4] / len(zhihoujieshu)
b = counts[2] / len(zhihoujieshu)
c = a + b
modeval = counts[mode] / len(zhihoujieshu)


print(f'众数是 {mode}，它在数组中的占比是 {modeval}')
print(f'滞后区间那两个点的滞后阶数占比之和是 {c}')

print(zhihoujieshu[20:22])
